<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class RegistrationApiController extends AbstractRestfulController
{
    public function getList()
    {			
			
    }
    public function get($user_id)
    {		
		header('Access-Control-Allow-Origin: *');
		$data=array();		
		$userTable=$this->getServiceLocator()->get('Models\Model\UserFactory');
		$userData = $userTable->getUserData($user_id)->toarray();
		if(count($userData)!=0){
			return new JsonModel(array(
				'data' 		=> json_encode($userData),
			));
		}else{
			return new JsonModel(array(
				'output' 	=> 'no data',
			));
		}
		/*
		$userData = $userTable->getUserData($user_id)->current();
		echo $_GET['callback'] . '({data:' . json_encode($userData) . '})';
		exit;
		*/
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UserFactory');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		$userDetailsTable=$this->getServiceLocator()->get('Models\Model\UserDetailsFactory');
		$module="";
		$userRegistration = $userTable->saveUserData($data);
		$userRegistrationDetails = $userDetailsTable->saveUserDetails($data,$userRegistration);
		$saveregistrationmotification = $notificationTable->sendAdminComplaint($data,'2','0','0','0','1');
		$module="http://aapthitech.com/demos/medicart/";
		$username=$data['inputFirstname'];
		$useremail=$data['inputEmail'];
		$userpassword=$data['password'];
		global $registerMessage;
		global $registerSubject;
		global $adminRegisterMessage;
		global $adminRegisterSubject;
		$registerMessage = str_replace("<USERNAME>","$username", $registerMessage);
		$adminRegisterMessage = str_replace("<ADMINUSERNAME>","$username", $registerMessage);
		$adminRegisterSubject = str_replace("<SUBADMINUSERNAME>","$username", $registerMessage);
		$registerMessage = str_replace("<USEREMAIL>","$useremail", $registerMessage);
		$registerSubject = str_replace("<TYPE>","User", $registerSubject);
		$registerMessage = str_replace("<USERPASSWORD>","$userpassword", $registerMessage);
		$registerMessage = str_replace("<LINK>","$module", $registerMessage);
		$to=$useremail;	
		$toadmin='admin@gmail.com';	
			if(sendMail($to,$registerSubject,$registerMessage)){
				if(sendMail($toadmin,$adminRegisterSubject,$adminRegisterMessage)){
					return new JsonModel(array(
						'output' 	=> 'success',
						'UserId'	=>	json_encode($userRegistration),
						'value'	    => 1,
					));
				}else{
					return new JsonModel(array(
						'output' 	=> 'not success',
						'value' 	=> 0,
					));
				}
			}else{
				return new JsonModel(array(
					'output' 	=> 'not success',
					'value' 	=> 0,
				));
			}
		
		/*if($userRegistrationDetails!=0){
			return new JsonModel(array(
				'output' 	=> 'success',
				'UserId'	=>	$userRegistration,
				'value'	    => 1,
			));
		}else{
			return new JsonModel(array(
				'output' 	=> 'not success',
				'value' 	=> 0,
			));
		}*/
    }
    public function update($uid, $data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UserFactory');
		$userDetailsTable=$this->getServiceLocator()->get('Models\Model\UserDetailsFactory');
		$userRegistration = $userTable->updateUserData($data,$uid);
		$userRegistrationDetails = $userDetailsTable->updateUserDetails($data,$uid);
		if($userRegistrationDetails){
			return new JsonModel(array(
				'output' 	=> 'success',
				'value' 	=> 1,
			));
		}else{
			return new JsonModel(array(
				'output' 	=> 'fail',
				'value' 	=> 0,
			));
		}
    }
    public function delete($id)
    {
       
    }
}